package ui_utils;


import scala.sys.process._
import scala.collection.mutable.HashMap;


class OPTION (val in_description : String, val in_command : String) {
  var description : String = in_description
  var commands = Array[String]() :+ in_command 

  def AddCommand (new_comm : String) = {
    commands = commands :+ new_comm
  }
}


class NoSuchOptionException extends Exception {}


class OPT_LIST {
  var options = Array[OPTION]()

  def apply(index : Int) = {
    options(index)
  }

  def length() = { 
    options.length
  }

  def RunOption (iin : Int) = {
    if (Range(0, options.length) contains iin) {
      println("Run Option ["+iin.toString+"]: ")
      for (acomd <- options(iin).commands) {
        println("-->> " + acomd)
        acomd !!
      }
    }
    else {
      throw new NoSuchOptionException()
    }
  }
  
  def AddOption (new_opt : OPTION) = {
    options = options :+ new_opt 
  }
}


object UI_UTILS {
  def GetIntsFromConsole (in_prompt : String) = {
    def isInt (sin : String) = {
      try {
	sin.toInt
	true
      } catch { 
	case _ : java.lang.NumberFormatException => false
      }
    }
    
    print(in_prompt)
    val instring = readLine()
    var inints = Array[Int]()

    instring.split(' ').foreach(
      isin => isInt(isin) match {
	case true => (inints contains isin.toInt) match {
	  case true => ;
	  case false => inints = inints :+ isin.toInt
	}
	case false => ;
      }
    )	  
    
    inints
  }

  def DisplayOptions (opts : OPT_LIST, tailfunc : => Seq[Int]) = {
    for (i <- 0 until opts.length()) {
      println("["+i.toString+"] "+opts(i).description)
    }

    tailfunc
  }
}
